<?php
namespace App\Controllers;
use App\Requests\UploadImageRequest;
use App\Requests\UploadVideoRequest;
use Framework\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use OSS\OssClient;
use OSS\Core\OssException;

class UploadOssController extends BaseController
{

    protected $accessKeyId;
    protected $accessKeySecret;
    protected $endpoint;
    protected $bucket;

    public function __construct()
    {
        $this->accessKeyId = config('filesystems.OSS_ACCESS_KEY');
        $this->accessKeySecret = config('filesystems.OSS_SECRET_KEY');
        $this->endpoint = config('filesystems.OSS_ENDPOINT');
        $this->bucket = config('filesystems.OSS_BUCKET');
    }

    public function upload(Request $request)
    {
        $request->validate([
            'file' => 'required|file|mimes:jpeg,jpg,png,mp4|max:20480', // 最大20MB
        ]);
        $file = $request->file('file');

        if (App::environment('local')) {
            $re = $this->uploadLocal($file);
        }else{
            $extension = $file->getClientOriginalExtension();
            if ($extension === 'jpg' || $extension === 'png' || $extension ==='jpeg') {

                $objectName = $file->getClientOriginalName();
                $filePath = $file->getRealPath();
                $re = $this->uploadImage($objectName,$filePath);

            } elseif ($extension === 'mp4') {

                $object = 'video/' . uniqid() . '.mp4';
                $filePath = $file->getRealPath();
                $re = $this->uploadVideo($object,$filePath);

            } else {
                $this->retError("文件类型不支持,默认支持jpeg,png,mp4");
            }
        }

        $this->retSuccess($re);
    }


    /**  图片上传
     * @param $objectName
     * @param $filePath
     * @return string
     */
    public function uploadImage($objectName,$filePath)
    {

        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);

            $ossClient->uploadFile($this->bucket, $objectName, $filePath);

            return $this->getObjectUrl($objectName);
        } catch (OssException $e) {
            return "上传oss失败".$e->getMessage();
        }

    }

    /**视频上传
     * @param $object
     * @param $filePath
     */
    public function uploadVideo($object,$filePath)
    {
//        // 转换视频格式为 MP4
//        $ffmpeg = FFMpeg\FFMpeg::create();
//        $video = $ffmpeg->open($filePath);
//        $format = new FFMpeg\Format\Video\X264('aac');
//        $format->setAudioCodec('libfaac');
//        $format->setVideoCodec('libx264');
//        $format->setVideoFrameRate(25);
//        $format->setVideoPixelFormat('yuv420p');
//        $format->setAudioChannels(2);
//        $format->setAudioKiloBitrate(128);
//        $format->setVideoKiloBitrate(800);
//        $format->setVideoResolution(640, 360);
//        $video->save($format, storage_path('app/public') . '/' . $object);

        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->uploadFile($this->bucket, $object, $object, $filePath);
//            unlink(storage_path('app/public') . '/' . $object);
            return $this->getObjectUrl($object);
        } catch (OssException $e) {
            return "上传oss失败".$e->getMessage();
        }

    }


    /** 上传本地
     * @param Request $request
     */
    public function uploadLocal($flie)
    {
        $path = $flie->store('avatars');
        $url = env('APP_URL').'/app/'.$path;
        return $url;
    }


    public function getObjectUrl($objectName)
    {
        return 'https://' . $this->bucket . '.' . $this->endpoint . '/' . $objectName;
    }



}
